#include "sessionfriendarea.h"
#include <QPushButton>
#include <QPainter>
#include <QScrollBar>
#include <QVBoxLayout>
#include <QStyleOption >
#include "debug.h"
#include "model/data.h"
#include "messageshowarea.h"
SessionFriendArea::SessionFriendArea(QWidget *parent)
    : QScrollArea{parent}, lastSelectedItem(nullptr)
{
    //必须要设置这个属性才可以
    setWidgetResizable(true);
    //设置滚动条样式
    this->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0px}");
    this->verticalScrollBar()->setStyleSheet(
        "QScrollBar:vertical {"
        "    border: none;"
        "    background: #f1f1f1;"  // 滚动条轨道颜色
        "    width: 6px;"         // 稍微宽一点更易点击
        "    margin: 0px 0px 0px 0px;"
        "}"
        "QScrollBar::handle:vertical {"
        "    background: #c1c1c1;" // 滚动条手柄颜色
        "    min-height: 20px;"    // 最小高度
        "    border-radius: 3px;"  // 圆角
        "}"
        "QScrollBar::handle:vertical:hover {"
        "    background: #a8a8a8;" // 悬停颜色
        "}"
        "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"
        "    height: 0px;"         // 隐藏上下箭头
        "    background: none;"
        "}"
        "QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {"
        "    background: none;"
        "}"
        );
    //创建核心的widget
    container = new QWidget();
    container->setFixedWidth(270);
    this->setWidget(container);

    //设置布局
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setContentsMargins(0, 12, 0, 0);
    layout->setSpacing(0);
    layout->setAlignment(Qt::AlignTop);  //元素靠上
    container->setLayout(layout);

#if TEST_UI
    QIcon icon(":/resource/images/avatar.jpg");
    for(int i = 0; i < 60; ++i)
    {
        addItem(SESSION_TYPE, this, QString::number(i), icon, "liaorui", "");
    }
#endif
}

void SessionFriendArea::addItem(SessionType type, QWidget *owner, const QString &id, const QIcon &avatar, const QString &name, const QString &text)
{
    SessionFriendAreaItem* item = nullptr;
    if(type == SESSION_TYPE)
    {
        item = new SessionItem(owner, id, avatar, name, text);
    }
    else if(type == FRIEND_TYPE)
    {
        item = new FriendItem(owner, id, avatar,name,  text);
    }
    else if (type == APPLY_TYPE)
    {
        item = new ApplyItem(owner, id, avatar, name);
    }
    else
    {
        LOG() << "未知类型, type : " << type;
        return;
    }
    this->container->layout()->addWidget(item);
}

SessionFriendAreaItem::SessionFriendAreaItem(QWidget *owner, const QIcon &avatar,
                                             const QString &name, const QString &text)
    :selected(false),owner(owner)
{
    //设置item的参数
    this->setFixedHeight(70);

    this->setStyleSheet("QWidget {background-color: rgb(232, 231, 230);}");
    //设置布局
    layout = new QGridLayout();
    layout->setContentsMargins(20, 0, 0, 0);
    layout->setSpacing(0);
    this->setLayout(layout);
    //头像按钮
    QPushButton* avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(QSize(50, 50));
    avatarBtn->setIcon(avatar);
    avatarBtn->setIconSize(QSize(50, 50));
    avatarBtn->setStyleSheet("QPushButton {border: none;}");
    avatarBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);


    QLabel* nameLabel = new QLabel();
    nameLabel->setText(name);
    nameLabel->setStyleSheet("QLabel {"
                             "   font-size: 16px;"                   // 字体大小
                             "   color: #000000;"                    // 黑色文字
                             "   padding-left: 5px;"                  // 内边距
                             "   background-color: transparent;"     // 透明背景
                             "}");

    lastMessage = new QLabel();
    lastMessage->setText(text);

    lastMessage->setStyleSheet("QLabel {padding-left: 5px;}");


    //设置布局
    layout->addWidget(avatarBtn, 0, 0, 2, 2);
    layout->addWidget(nameLabel, 0, 2, 1,8);
    layout->addWidget(lastMessage, 1, 2, 1,8);
}

void SessionFriendAreaItem::select()
{
    //获取自己的兄弟
   // const QObjectList& parentList = this->parentWidget()->children();

   // for(auto & item: parentList)
   // {
   //     if(!item->isWidgetType())
   //     {
   //         continue;
   //     }
   //     SessionFriendAreaItem* widget = dynamic_cast<SessionFriendAreaItem*>(item);
   //     widget->clear();
   // }

   // //换成自己的样式
   // selected = true;
   // this->setStyleSheet("QWidget { background-color: rgb(210, 209, 209); }");
   // //处理right界面的变化
   // this->active();
   //换一个版本， 这个效率太慢了
    SessionFriendArea* area = dynamic_cast<SessionFriendArea* >(owner);
    if(area->lastSelectedItem != nullptr)
    {
        //清除子集的样式
        area->lastSelectedItem->clear();
    }
    // 设置当前选中的样式
    selected = true;
    area->lastSelectedItem = this; //保存最近修改的变量
    this->setStyleSheet("QWidget { background-color: rgb(210, 209, 209); }");
    this->active();



}


//清除选中状态
void SessionFriendAreaItem::clear()
{
    selected = false;
    this->setStyleSheet("QWidget {background-color: rgb(232, 231, 230);}");
}

void SessionFriendAreaItem::enterEvent(QEnterEvent *event)
{
    (void) event;
    if(!selected)
    {
        this->setStyleSheet("QWidget {background-color: rgb(215, 215,215);}");
    }
}

void SessionFriendAreaItem::leaveEvent(QEvent *event)
{
    (void) event;
    if(!selected)
    {
        this->setStyleSheet("QWidget {background-color: rgb(232, 231, 230);}");
    }
}

void SessionFriendAreaItem::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void SessionFriendAreaItem::mousePressEvent(QMouseEvent *event)
{
    (void)event;
    select();

}

void SessionFriendAreaItem::active()
{
    //子类重写即可
}

//创建常用组件

//////////////////////////////////////////////////////////
/// 会话 Item 的实现
//////////////////////////////////////////////////////////
SessionItem::SessionItem(QWidget *owner, const QString &sessionId, const QIcon &avatar, const QString &name, const QString &lastMessage)
    :SessionFriendAreaItem(owner, avatar, name, lastMessage), sessionId(sessionId)
{

}

void SessionItem::active()
{
    LOG() << "选中会话item,  sessionid： " << sessionId;
}



//////////////////////////////////////////////////////////
/// 好友 Item 的实现
//////////////////////////////////////////////////////////
FriendItem::FriendItem(QWidget *owner, const QString &id, const QIcon &avatar, const QString &name, const QString &descripition)
    :SessionFriendAreaItem(owner, avatar, name, descripition), userId(id)
{

}

void FriendItem::active()
{
    LOG() << "选中好友item,  userId ： " << userId;
}

//////////////////////////////////////////////////////////
/// 还有申请 Item 的实现
//////////////////////////////////////////////////////////
ApplyItem::ApplyItem(QWidget *owner,const QString& id,  const QIcon &avatar, const QString &name)
    :SessionFriendAreaItem(owner, avatar, name, ""), userId(id)
{
    //1、移除lastMessage
    layout->removeWidget(lastMessage);
    delete lastMessage;

    //2. 添加按钮
    QPushButton* acceptBtn = new QPushButton();
    acceptBtn->setText("同意");

    QPushButton* rejectBtn = new QPushButton();
    rejectBtn->setText("拒绝");
    //3. 添加到布局管理器
    layout->addWidget(acceptBtn, 1, 2, 1, 1);
    layout->addWidget(rejectBtn, 1, 3, 1, 1);
}

void ApplyItem::active()
{
    LOG() << "选中添加好友item,  userId： " << userId;
}
